/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.mapper.BookMapper;
import cn.webrx.mapper.DbMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Project: mybatis2022 - App
 * <p>Powered by webrx On 2022-04-15 14:26:20
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@SpringBootApplication
@RestController
@EnableCaching
@MapperScan("cn.webrx.mapper")
public class App implements ApplicationRunner {
    @Value("${server.port:8080}")
    private int port;

    @Autowired
    DbMapper dm;

    @Autowired(required = false)
    BookMapper bm;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping  @Cacheable(value = "default", key = "#id")
    public Map<String, Object> index(@RequestParam(name = "id",required = false,defaultValue = "111") Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "查询成功");
        map.put("data", bm.selectAll());
        map.put("sver", SpringBootVersion.getVersion());
        return map;
    }


    @GetMapping("/clear")
    @CacheEvict(value = "default", key = "111")
    public Map<String, Object> clear() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "200");
        map.put("msg", "清除缓存default key=111");
        return map;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            Runtime.getRuntime().exec("cmd /k start http://localhost:" + port);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
